/*
* $Id: MainWin.cpp 143 2010-09-09 19:55:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "MainWin.h"

#pragma warning(disable: 4407)

#define CLOSE_ID		0x1001
#define FOLD_ID			0x1002

CMainWin::CMainWin(const std::vector<IVisualComponent*>& aVisualComponents)
: m_CloseButton(IDB_CLOSE, _T("")),
m_FoldButton(IDB_FOLD, _T("")){
	m_bDownMouseButton = false;
	m_aComponentButtons = aVisualComponents;
	m_pFont = CCommonFunction::GetFont(_T("Book Antiqua"), 22, TRUE, TRUE);
}
CMainWin::~CMainWin(){
	m_pFont->DeleteObject();
	m_pBitmapLogo->DeleteObject();
	m_pBitmapHeader->DeleteObject();
	delete m_pFont;
	delete m_pBitmapHeader;
	delete m_pBitmapLogo;
}

BEGIN_MESSAGE_MAP(CMainWin, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_DESTROY()
	ON_WM_TIMER()
END_MESSAGE_MAP()

BOOL CMainWin::OnCommand(WPARAM wParam, LPARAM lParam){
	BOOL bRes = __super::OnCommand(wParam, lParam);
	if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == CLOSE_ID)
		PostMessage(WM_CLOSE, 0, 0);
	if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == FOLD_ID)
		CloseWindow();
	for(std::vector<IVisualComponent*>::iterator it = m_aComponentButtons.begin(); it != m_aComponentButtons.end(); it++){
		if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == (*it)->GetButtonInMainWindow()->GetWinId())
			(*it)->CreateComponentFrame();
	}
	return bRes;
}
int CMainWin::OnCreate(LPCREATESTRUCT lpCreateStruct){
	CRgn rgn;
	CRect rect;
	GetClientRect(rect);
	CSize size = rect.Size();
	int x = (lpCreateStruct->cx - size.cx)/2;
	int y = lpCreateStruct->cy - size.cy - x;
	rgn.CreateRectRgnIndirect(CRect(CPoint(x, y), size));
	SetWindowRgn(rgn, TRUE);

	SetFont(m_pFont, TRUE);

	rect.top = 30;
	m_pBitmapLogo = new CBitmap;
	m_pBitmapLogo->LoadBitmap(IDB_LOGO);
	rect.top = 0;
	rect.bottom = 30;
	m_pBitmapHeader = new CBitmap;
	m_pBitmapHeader->LoadBitmap(IDB_HEADER);

	m_CloseButton.Create(CRect(rect.right - 18, 0, rect.right, 30), this, CLOSE_ID);
	m_FoldButton.Create(CRect(rect.right - 36, 0, rect.right - 18, 30), this, FOLD_ID);

	for(std::vector<IVisualComponent*>::iterator it = m_aComponentButtons.begin(); it != m_aComponentButtons.end(); it++)
		(*it)->GetButtonInMainWindow();
	SetTimer(1, 10, NULL);
	return TRUE;
}
void CMainWin::OnTimer(UINT_PTR){
	SHORT h= GetKeyState(VK_LBUTTON);
	if(!HIBYTE(h) && m_bDownMouseButton)
		m_bDownMouseButton = false;
	if(m_bDownMouseButton){
		CRect rect;
		GetWindowRect(rect);
		CPoint pnt;
		GetCursorPos(&pnt);
		rect.OffsetRect(CPoint(pnt.x - m_pointMouse.x, pnt.y - m_pointMouse.y));
		m_pointMouse = pnt;
		MoveWindow(&rect, TRUE);
	}
}
void CMainWin::OnLButtonDown(UINT, CPoint){
	m_bDownMouseButton = true;
	GetCursorPos(&m_pointMouse);
}
void CMainWin::OnPaint(){
	CPaintDC dc(this);
	COLORREF colorBk = RGB(122, 210, 248);
	CBrush hBrush;
	hBrush.CreateSolidBrush(colorBk);
	CRect rect;
	GetClientRect(rect);
	dc.FillRect(&rect, &hBrush);
	CDC cdc;
	cdc.CreateCompatibleDC(&dc);
	CBitmap Bitmap;
	Bitmap.CreateCompatibleBitmap(&dc, 450, 505);
	HGDIOBJ bitmapObj = cdc.SelectObject(Bitmap);
	cdc.DrawState(CPoint(0, 0), CSize(450, 30), m_pBitmapHeader, DST_BITMAP | DSS_NORMAL, NULL);
	cdc.DrawState(CPoint(0, 30), CSize(205, 475), m_pBitmapLogo, DST_BITMAP | DSS_NORMAL, NULL);
	dc.StretchBlt(0, 0, rect.Size().cx, 30, &cdc, 0, 0, 450, 30, SRCCOPY);
	dc.StretchBlt(0, 30, (rect.Size(). cy - 30) * 205/475, rect.Size().cy - 30, &cdc, 0, 30, 205, 475, SRCCOPY);
	cdc.SelectObject(bitmapObj);
	Bitmap.DeleteObject();
	cdc.DeleteDC();
	dc.DrawIcon(CPoint(0, 0), LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MAINFRAME)));
	HGDIOBJ fontObj = dc.SelectObject(m_pFont->m_hObject);
	dc.SetBkMode(TRANSPARENT);
	dc.DrawText(ILanguageManager::GetLanguageManager()->GetString(MAIN_FRAME_CAPTION_ID).c_str(), 6, CRect(35, 0, 100, 30), DT_CENTER | DT_SINGLELINE | DT_VCENTER);
	dc.SelectObject(fontObj);
}

void CMainWin::OnDestroy(){
	KillTimer(1);
	PostQuitMessage(0);
}
void CMainWin::Free(){
	delete this;
}